package com.jinlejia.bbs.logic.message;

import android.content.Context;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.bean.MessageCountBean;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.HashMap;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;
/**
 * @author 王进 on 2019/3/26.
 */

public class MessageLogic {
    private Context context;
    private LogicCallBack callBack;
    public MessageLogic(Fragment fragment, LogicCallBack callBack){
        this.context = fragment.getContext();
        this.callBack = callBack;
    }

    public void execute(){
        JSONObject json = new JSONObject();
        httpCallForm(BBSConfig.countMessageByType,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(null);
                    }
                    @Override
                    public void onError(Exception e) {
                        if (callBack==null)return;
                        callBack.onResult(null);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        if (callBack==null)return;
                        HashMap map = parse.parse(HashMap.class);
                        MessageCountBean bean = new MessageCountBean();
                        //1 回答 2 评论 3 点赞 4 邀请回答
                        bean.setAnswerCount(Integer.parseInt(String.valueOf(map.get("1"))));
                        bean.setCommentCount(Integer.parseInt(String.valueOf(map.get("2"))));
                        bean.setLikeCount(Integer.parseInt(String.valueOf(map.get("3"))));
                        bean.setInviteCount(Integer.parseInt(String.valueOf(map.get("4"))));
                        callBack.onResult(bean);
                    }
                }));
    }

    public interface LogicCallBack{
        void onResult(MessageCountBean bean);
    }
}
